package com.dews.framework.config.redis;

import java.nio.charset.StandardCharsets;

import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;

public class StringKeyPrefixRedisSerializer extends StringRedisSerializer {

    private String keyPrefix;


    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }


    @Override
    public String deserialize(@Nullable byte[] bytes) {
        String string = new String(bytes, StandardCharsets.UTF_8);
        string = keyPrefix + string;
        return (bytes == null ? null : new String(bytes, StandardCharsets.UTF_8));
    }

    @Override
    public byte[] serialize(@Nullable String string) {
        string = keyPrefix + string;
        return (string == null ? null : string.getBytes(StandardCharsets.UTF_8));
    }

}
